package j.quartz.ui.controller.sys;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import j.quartz.sys.user.SysUserService;
import j.quartz.sys.user.entity.SysUser;
import j.quartz.ui.common.utils.SysUserUtil;
import j.quartz.ui.sys.shiro.realm.SysUserRealm;

@Controller
public class SysController {
	
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;	
	
	@Autowired
	private SysUserService sysUserService ;
	
	/**
	 * /login 登录成功后,跳转到该请求,跳转到首页
	 */
	@RequestMapping("/index")
	public String index(ModelAndView view) {
		ValueOperations<String,String> opsForValue = redisTemplate.opsForValue();
		
		return "/index";
	}
	
	
	/**
	 * 进入admin后台首页
	 */
	@RequestMapping("/admin")
	public ModelAndView admin() {
		
		ModelAndView view = new ModelAndView("sys/admin");
		
		
		return view ;
	}
	
	
	/**
	 * 登录动作
	 */
	@RequestMapping("/login1")
	public ModelAndView login1(@RequestParam("username") String username,
							  @RequestParam("password") String password
							  ) {
		// 如果已经登录，则跳转到管理首页
		SysUserRealm.Principal principal = SysUserUtil.getPrincipal(); 
		if (principal != null && !principal.isMobileLogin()) {
			return new ModelAndView("redirect:/admin");
		}
				
		ModelAndView view = new ModelAndView() ;
		
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (IncorrectCredentialsException ice) {
            // 捕获密码错误异常
            ModelAndView mv = new ModelAndView("error");
            mv.addObject("message", "password error!");
            return mv;
        } catch (UnknownAccountException uae) {
            // 捕获未知用户名异常
            ModelAndView mv = new ModelAndView("error");
            mv.addObject("message", "username error!");
            return mv;
        } catch (ExcessiveAttemptsException eae) {
            // 捕获错误登录过多的异常
            ModelAndView mv = new ModelAndView("error");
            mv.addObject("message", "times error");
            return mv;
        }
        SysUser user = sysUserService.getOneByUsername(username);
        subject.getSession().setAttribute("sysUser", user);
		
		return new ModelAndView("success");
	}
	
	
	
	
	

}
